//
//  GROrderTraceView.m
//  YC_GRice
//
//  Created by luojinrong11 on 2018/7/25.
//  Copyright © 2018年 YeCai. All rights reserved.
//

#import "GROrderTraceView.h"
#import "GRReturnTimeCell.h"
#import "GRReturnResonModel.h"
@interface GROrderTraceView()<UITableViewDelegate,UITableViewDataSource>
@property (weak, nonatomic) IBOutlet UITableView *orderTraceList;
@property(nonatomic,strong)NSMutableArray * sourArr;

@property (weak, nonatomic) IBOutlet UIView *placeView;

@end


@implementation GROrderTraceView

-(NSMutableArray *)sourArr{
    if (!_sourArr) {
        _sourArr = [NSMutableArray array];
    }
    return _sourArr;
}
-(void)awakeFromNib{
[super awakeFromNib];
    self.orderTraceList.delegate = self;
    self.orderTraceList.dataSource = self;
    UITapGestureRecognizer * tapGes = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(hiddenViewAction:)];
    [self.placeView addGestureRecognizer:tapGes];
    [self.orderTraceList registerNib:[UINib nibWithNibName:@"GRReturnTimeCell" bundle:[NSBundle mainBundle]] forCellReuseIdentifier:@"timeCell"];
}

-(void)hiddenViewAction:(UITapGestureRecognizer *)recognized{
    [self removeFromSuperview];
}

+(instancetype)shareInstance{
    static GROrderTraceView * traceView = nil;
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        traceView = [GROrderTraceView  xx_instantiateFromNib];
    });
    return traceView;
}
+(void)show:(NSArray *)dataArr{
    GROrderTraceView * traceView =   [GROrderTraceView  shareInstance];
    [traceView reload:dataArr];
    UIWindow * window = [UIApplication sharedApplication].keyWindow;
    [window addSubview:traceView];
    [traceView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.edges.equalTo(window);
    }];
}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    GRReturnTimeCell * timeCell = [tableView dequeueReusableCellWithIdentifier:@"timeCell" forIndexPath:indexPath];
    if (indexPath.row == 0) {
        timeCell.topLine.hidden = YES;

    }else{
        timeCell.topLine.hidden = NO;
    }
    if (indexPath.row == _sourArr.count -1 ) {
        timeCell.bottomLine.hidden = YES;
        timeCell.colorView.hidden = NO;
        timeCell.titleLabel.textColor = HEXCOLOR(0x222222);
    }else{
        timeCell.bottomLine.hidden = NO;
        timeCell.colorView.hidden = YES;
        timeCell.titleLabel.textColor = HEXCOLOR(0x999999);
   
    }
    GRLogsticProgresModel * model = self.sourArr[indexPath.row];
    timeCell.titleLabel.text = model.msg;
    timeCell.leftTimLabel.text = model.time;
    return timeCell;
}

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return self.sourArr.count;
}

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return 60;
}

-(void)reload:(NSArray *)dataArr{
    [self.sourArr removeAllObjects];
    [self.sourArr addObjectsFromArray:dataArr];
    [self.orderTraceList reloadData];
    
}

- (IBAction)closeAction:(id)sender {
    [self removeFromSuperview];
}


@end
